﻿using EC18_ESHOP.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace EC18_ESHOP.Controllers
{
    public class ProductDetailController : Controller
    {
        //
        // GET: /ProductDetail/
        EShopEntities db = new EShopEntities();

        public ActionResult Index(int id)
        {
            PRODUCT product = db.PRODUCT.Find(id);
            if (product != null)
            {
                ViewBag.Result = true;
                ViewBag.Product = product;
                ViewBag.Title = product.ProductName;
                //lấy hết đường dẫn hình ảnh gán vào list
                List<string> urlImages = getListUrlImages(product.ProductId);
                ViewBag.ListUrlImages = urlImages;

                ViewBag.ListProperty = product.PRODUCT_PROPERTIES.ToList();
                //db.PRODUCT_PROPERTIES.Where(p => p.PropertyId == product.Product_ID).ToList();
            }
            else
                ViewBag.Result = false;
            return View();
        }

        public List<string> getListUrlImages(string productId)
        {
            List<string> _Url = new List<string>();
            string _root = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
            DirectoryInfo _direc = new DirectoryInfo(HostingEnvironment.MapPath(@"~\Images\Product\" + productId));
            var file = _direc.GetFiles("*.jpg").ToList();
            for (int i = 0; i < file.Count; i++)
            {
                string temp = file[i].ToString();
                if (temp.Substring(temp.Length - 5, 1) != "b")
                    _Url.Add(_root + "/Images/Product/" + productId + "/" + file[i].ToString());
            }
            return _Url;
        }

        //
        // POST: /ProductDetail/rating
        public JsonResult rating(int productId, string cmtTitle, string cmtMessage, int rating)
        {
            int userId = WebSecurity.GetUserId(User.Identity.Name);
            if (userId != -1)
            {
                PRODUCT product = db.PRODUCT.Find(productId);
                if (product != null)
                {
                    COMMENT cmt = new COMMENT();
                    cmt.Rating = rating;
                    cmt.UserId = userId;
                    cmt.ProductId = productId;
                    cmt.CmtMess = cmtMessage;
                    cmt.CmtTitle = cmtTitle;
                    cmt.CmtTime = DateTime.Now;
                    db.COMMENT.Add(cmt);
                    int quantityRating = product.COMMENT.Count();
                    if (product.TotalRating == 0)
                        product.TotalRating = rating;
                    else 
                        product.TotalRating = (product.TotalRating * quantityRating + rating) / (quantityRating + 1);
                    db.SaveChanges();
                }
                return Json(new { status = true }, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { status = false }, JsonRequestBehavior.AllowGet);
        }
    }
}